<?php
require_once dirname(__FILE__) . '/../../renew/controllers/BaseController.php';

/**
 * 【SP版】共通 コントローラ
 */
abstract class Mobileapi_BaseController extends Renew_BaseController
{

    /**
     * 初期処理
     */
    public function preDispatch()
    {
        $module_names = array(
            'Search',
            'Rstdtl',
            'Rstobi',
            'Shop'
        );

        // インスタンス生成
        foreach ($module_names as $module_name)
        {
            $params = array(
                'namespace' => $module_name,
                'basePath' => APPLICATION_PATH . '/modules/' . strtolower($module_name)
            );

            $loader = new Zend_Application_Module_Autoloader($params);
        }
    }

    /**
     * テンプレート出力処理
     *
     * @param $typ テンプレート名
     * @param $topbar テンプレート名（画面上部）
     */
    protected function displayBody($typ, $topbar)
    {
        $contents = $this->getViewScriptPath($typ);
        // $header = $this->getViewHelperPath('header_'.$typ);
        // $footer = $this->getViewHelperPath('footer_'.$typ);

        $this->display($contents);
        // $this->display($contents,$header,$footer,$topbar);
    }

    /**
     * テンプレートパス取得
     *
     * @param $folder フォルダ名
     * @param $typ テンプレート名
     * @return string テンプレートパス
     */
    protected function getViewPath($typ, $name)
    {
        $path = APPLICATION_PATH . '/modules/mobileapi/views/';
        $path .= $this->getRequest()->getControllerName();
        return $path . '/' . $typ . '/' . $name . '.tpl';
    }

    /**
     * 検討リスト取得
     */
    protected function loadData()
    {
        $params = $this->getParameters();
        $kentoList = $_COOKIE['kentoList'];

        if (!isset($kentoList) || empty($kentoList) || $kentoList == '')
        {
            $this->_data['NoData'] = true;
            return;
        }

        $model = new Mobileapi_Model_BukkenList();
        $model_conv = new Rstobi_Model_Converte();
        $model_common = new Rstobi_Model_Common();

        $from = $model_common->getCampFrom($params);

        $dbParams = array();

        $kentos = explode(',', $kentoList);

        $data = $model->getBukenByCds(implode(',', $kentos), $from, $dbParams);
        $model_conv->dataConverteForBuken($data, $params, false, false);

        $prev = NULL;
        $c = -1;
        foreach ($data as $d)
        {
            $tatemono = $d['T03_DAIHYO_TATEMONO_CD'];
            if ($prev != $tatemono)
            {
                $c++;
                $items[] = array();
            }
            $d['MAX_CHINRYO_STR'] = (intval($d['MAX_CHINRYO']) / 10000);
            $d['MIN_CHINRYO_STR'] = (intval($d['MIN_CHINRYO']) / 10000);
            // OEM判定
            $OemFlg = ((array_key_exists('OemCd', $params) == true) && (is_null($params['OemCd']) == false) && (is_numeric($params['OemCd']) == true));
            // 物件画像取得
            $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
            $d['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon($d['BUKEN_IMAGE']);
            // キャンペーン取得
            $heyaCd = $d['T03_HEYA_CD'];
            $ret = $model->getCamp($heyaCd);
            if (!empty($ret))
            {
                $d['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                $d['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                $d['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
            }

            $items[$c][] = $d;
            $prev = $tatemono;
        }

        $this->_data['list'] = $items;
    }

}
